var util = require('../../utils/util.js');
var api = require('../../config/api.js');

var app = getApp()

Page({
    data: {
        keyword: '',
        searchStatus: false,
        goodsList: [],
        helpKeyword: [],
        historyKeyword: [],
        currentSort: 'default',
        currentSortType: 'default',
        currentSortOrder: 'desc',
        filterCategory: [],
        defaultKeyword: {},
        hotKeyword: [],
        page: 1,
        size: 15,
        categoryId: 0,
        totalPages: 1
    },
    //事件处理函数
    closeSearch: function () {
        console.log("closeSearchcloseSearch")
        wx.navigateBack()
        this.setData({
            keyword: ''
        })
    },

    clearKeyword: function () {
        this.setData({
            keyword: '',
            searchStatus: false
        });
    },

    onLoad: function (e) {
        wx.removeStorageSync("goodsList");
        this.getSearchKeyword();
        console.log("进来了onLoadonLoadonLoadonLoad")
        if (e.keyword != null) {
            this.getSearchResult(e.keyword);
        }

    },

    getSearchKeyword() {
        let that = this;
        util.request(api.SearchIndex).then(function (res) {
            if (res.errno === 0) {
                that.setData({
                    historyKeyword: res.data.historyKeywordList,
                    defaultKeyword: res.data.defaultKeyword,
                    hotKeyword: res.data.hotKeywordList
                });
            }
        });
    },

    inputChange: function (e) {
        if (e.detail.value) {
            this.getHelpKeyword();
        }
    },

    getHelpKeyword: function () {
        let that = this;
        util.request(api.SearchHelper, {
            keyword: that.data.keyword
        }).then(function (res) {
            if (res.errno === 0) {
                that.setData({
                    helpKeyword: res.data
                });
            }
        });
    },

    inputFocus: function () {
        this.setData({
            searchStatus: false,
            goodsList: []
        });

        if (this.data.keyword) {
            this.getHelpKeyword();
        }
    },

    clearHistory: function () {
        this.setData({
            historyKeyword: []
        })

        util.request(api.SearchClearHistory, {}, 'POST')
            .then(function (res) {
                console.log('清除成功');
            });
    },

    getGoodsList: function () {
        let that = this;
        let keyword = this.data.keyword;
        if (keyword != null) {
            util.request(api.GoodsList, {
                keyword: that.data.keyword,
                page: that.data.page,
                size: that.data.size,
                sort: that.data.currentSort

            }).then((res) => {
                if (res.errno === 0) {
                    that.setData({
                        searchStatus: true,
                        goodsList: that.data.goodsList.concat(res.data.goodsList),
                        totalPages: res.data.totalPages
                    });
                    wx.setStorageSync("goodsList", that.data.goodsList);
                }

                //重新获取关键词
                that.getSearchKeyword();
            });
        }
    },

    onKeywordTap: function (event) {

        this.getSearchResult(event.target.dataset.keyword);

    },

    getSearchResult(keyword) {
        if (keyword === '') {
            keyword = null;
        }
        this.setData({
            keyword: keyword,
            page: 1,
            totalPages: 1,
            categoryId: 0,
            goodsList: []
        });

        this.getGoodsList();
    },

    openSortFilter: function (event) {
        let currentId = event.currentTarget.id;
        switch (currentId) {
            case 'priceSort':
                let tmpSortOrder = 'asc';
                if (this.data.currentSortOrder === 'asc') {
                    tmpSortOrder = 'desc';
                }
                this.setData({
                    currentSortType: 'priceSort',
                    currentSort: 'price_asc',
                    currentSortOrder: tmpSortOrder,
                    categoryFilter: false,
                    goodsList: []
                });
                // wx.removeStorageSync("goodsList")
                this.getGoodsList();
                // this.arrSort();
                break;

            case 'total_sales':
                this.setData({
                    currentSortType: 'total_sales',
                    currentSort: 'total_sales_des',
                    currentSortOrder: 'asc',
                    categoryFilter: false,
                    goodsList: []
                });
                wx.removeStorageSync("goodsList")
                this.getGoodsList();
                // this.arrSort();
                break;

            case 'tk_rate':
                this.setData({
                    currentSortType: 'tk_rate',
                    currentSort: 'tk_rate_asc ',
                    currentSortOrder: 'asc',
                    categoryFilter: false,
                    goodsList: []
                });
                wx.removeStorageSync("goodsList")
                this.getGoodsList();
                // this.arrSort();
                break;
            default:
                //综合排序
                this.setData({
                    currentSortType: 'default',
                    currentSort: 'default',
                    currentSortOrder: 'desc',
                    categoryFilter: false,
                    categoryId: 0,
                    goodsList: []
                });
                wx.removeStorageSync("goodsList")
                this.getGoodsList();
            // this.arrSort();
        }
    },


    onKeywordConfirm(event) {
        if (this.data.keyword !== "" && this.data.keyword !== event.detail.value) {
            wx.setStorageSync("goodsList", null);
        }
        this.getSearchResult(event.detail.value);
    },

    onReachBottom: function () {
        if (this.data.totalPages > this.data.page) {
            this.setData({
                page: this.data.page + 1
            });
        } else {
            wx.showToast({
                title: '已经到底了!',
                icon: 'none',
                duration: 2000
            });
            return false;
        }
        this.getGoodsList();
    },

    onShow() {
        if (this.data.keyword !== '') {
            this.getSearchResult(this.data.keyword);
        }
    },


})
